package com.deluxelx.rmo.messageboard.service;

import com.deluxelx.rmo.common.constants.MqConstants;
import com.deluxelx.rmo.common.enums.ResponseCode;
import com.deluxelx.rmo.common.message.UserSentimentMes;
import com.deluxelx.rmo.common.util.DateUtil;
import com.deluxelx.rmo.common.util.ToolWordsUtil;
import com.deluxelx.rmo.common.util.supportIds.policy.SnowFlakeId;
import com.deluxelx.rmo.messageboard.api.PostService;
import com.deluxelx.rmo.messageboard.api.dto.DiscussPostDto;
import com.deluxelx.rmo.messageboard.api.req.DiscussPostReq;
import com.deluxelx.rmo.messageboard.api.res.MessageBoardRes;
import com.deluxelx.rmo.messageboard.mapper.DiscussPostMapper;
import com.deluxelx.rmo.messageboard.mapper.PostImgMapper;
import com.deluxelx.rmo.messageboard.model.po.DiscussPost;
import com.deluxelx.rmo.user.api.dto.UserPostDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

import static com.deluxelx.rmo.common.constants.DiscussPostConstants.DEFAULT_NUM;
import static com.deluxelx.rmo.common.constants.DiscussPostConstants.DEFAULT_STATUS;

@Service
public class PostServiceImpl implements PostService {

    @Resource
    private DiscussPostMapper postMapper;

    @Resource
    private PostImgMapper postImgMapper;

    @Resource
    private SnowFlakeId snowFlakeId;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageBoardRes<String> sendPost(DiscussPostReq postReq, UserPostDto userPostDto) {
        // 1. 敏感词初步识别
        if (ToolWordsUtil.isIllegalWords(postReq.getContent())) {
            return MessageBoardRes.buildErrorResult(ResponseCode.ILLEGAL_WORDS);
        }

        List<String> imgList = postReq.getImgList();
        // TODO 后期有时间优化用 MongoDB
        // 2. 创建一个帖子
        DiscussPost discussPost = new DiscussPost();
        long postId = snowFlakeId.nextId();

        discussPost.setPostId(postId);
        discussPost.setUserId(postReq.getUserId());
        discussPost.setNickname(userPostDto.getNickname());
        discussPost.setAvatar(userPostDto.getAvatar());
        discussPost.setContent(postReq.getContent());
        discussPost.setPraiseNum(DEFAULT_NUM);
        discussPost.setCommentCount(DEFAULT_NUM);
        discussPost.setImgCount(imgList.size());
        discussPost.setStatus(DEFAULT_STATUS);
        // 2.1 将帖子插入数据库
        int postSuccess = postMapper.insertDiscussPost(discussPost);
        // 3. 在数据库内插入图片 URL
        int postImgSuccess = postImgMapper.insertPostImg(postId, imgList);
        // 4. 如果都插入成功，则发送异步消息。修改用户的情感指数
        if (postSuccess > 0 && postImgSuccess > 0) {
            UserSentimentMes sentimentMes = new UserSentimentMes(postReq.getUserId(), postReq.getContent());
            rabbitTemplate.convertAndSend(MqConstants.SENTIMENT_QUEUE, sentimentMes);
            return MessageBoardRes.buildSuccessResult("发布成功");
        }
        return MessageBoardRes.buildErrorResult("发布失败");
    }

    @Override
    public List<DiscussPostDto> getRecentPostList() {
        List<DiscussPost> discussPostList = postMapper.getRecentPost();
        List<DiscussPostDto> discussPostDtoList = new ArrayList<>();
        discussPostList.stream().peek(item -> {
            DiscussPostDto postDto = new DiscussPostDto();
            postDto.setPostId(item.getPostId());
            postDto.setNickname(item.getNickname());
            postDto.setAvatar(item.getAvatar());
            postDto.setContent(item.getContent());
            postDto.setPraiseNum(item.getPraiseNum());
            postDto.setCommentCount(item.getCommentCount());
            postDto.setPublishTime(DateUtil.getPostTime(item.getCreateTime()));
            discussPostDtoList.add(postDto);
        }).forEach(n -> {});
        return discussPostDtoList;
    }
}
